package com.ruoyi.bussine.service.common.impl;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.bussine.common.enums.order.DvyTypeEnum;
import com.ruoyi.bussine.common.enums.user.ProgramTypeEnum;
import com.ruoyi.bussine.domain.dto.wechat.*;
import com.ruoyi.bussine.domain.dto.wechat.miniPay.WechatPayPayerDTO;
import com.ruoyi.bussine.service.common.ModuleConfigService;
import com.ruoyi.bussine.service.common.WeChatBusinessService;
import com.ruoyi.bussine.service.integration.WeChatService;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author zhengyz
 * @date 2025/3/25
 */
@Slf4j
@Service
public class WeChatBusinessServiceImpl implements WeChatBusinessService {

    @Autowired
    private WeChatService weChatService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ModuleConfigService moduleConfigService;

    @Override
    public String getToken() {
        return getToken(ProgramTypeEnum.CONSUMER.getValue());
    }

    @Override
    public String getToken(String programType) {
        if (StringUtils.isBlank(programType)) {
            log.info("小程序类型为空，默认为消费者小程序");
            programType = ProgramTypeEnum.CONSUMER.getValue();
        }
        String token = redisCache.getCacheObject(CacheConstants.WECHAT_ACCESS_TOKEN + programType);
        if (token != null) {
            return token;
        }
        MiniProgramBaseInfoDTO miniProgramBaseInfoDTO = moduleConfigService.getMiniAppidAndSecret(programType);
        WeChatTokenRespDTO tokenRespDTO = weChatService.getToken(miniProgramBaseInfoDTO);
        if (tokenRespDTO == null) {
            throw new ServiceException("获取微信token失败");
        }
        redisCache.setCacheObject(CacheConstants.WECHAT_ACCESS_TOKEN + programType, tokenRespDTO.getAccessToken(), tokenRespDTO.getExpiresIn(), TimeUnit.SECONDS);
        return tokenRespDTO.getAccessToken();
    }

    @Override
    public String getUrlLink(WeChatUrlLinkReqDTO weChatUrlLinkReqDTO) {
        String token = getToken();
        return weChatService.generateUrlLink(token, weChatUrlLinkReqDTO);
    }

    @Override
    public byte[] getProgramQrCode(Map<String, Object> jsonMap) {
        String token = getToken();
        return weChatService.getwxacodeunlimit(token, jsonMap);
    }

    /**
     * @param order
     * @param openId
     */
    /*@Override
    public void sendOrder(TbOrder order, String openId) {
        ShippingInfoReqDTO shippingInfoReqDTO = new ShippingInfoReqDTO();

        ShippingInfoOrderKey shippingInfoOrderKey = new ShippingInfoOrderKey();
        shippingInfoOrderKey.setOrder_number_type(1);
        shippingInfoOrderKey.setTransaction_id(order.getThirdPayNo());
        shippingInfoOrderKey.setOut_trade_no(order.getOrderNo());
        shippingInfoOrderKey.setMchid(moduleConfigService.getWxMchId());

        ShippingList shippingList = new ShippingList();
        shippingList.setItem_desc(order.getProductName());

        WechatPayPayerDTO wechatPayPayerDTO = new WechatPayPayerDTO();
        wechatPayPayerDTO.setOpenid(openId);

        shippingInfoReqDTO.setOrder_key(shippingInfoOrderKey);
        if (DvyTypeEnum.SEND.getValue().equals(order.getDvyType())) {
            shippingInfoReqDTO.setLogistics_type(2);
        } else {
            shippingInfoReqDTO.setLogistics_type(4);
        }

        shippingInfoReqDTO.setShipping_list(Collections.singletonList(shippingList));
        shippingInfoReqDTO.setUpload_time(DateUtil.format(DateUtil.date(), "yyyy-MM-dd'T'HH:mm:ss+08:00"));
        shippingInfoReqDTO.setPayer(wechatPayPayerDTO);
        String token = getToken();
        weChatService.sendOrder(token, shippingInfoReqDTO);
    }*/
}
